﻿using CJiaBao.Core.Utils;
using Core.Configuration.Api;
using IconYuan.Manager.Articles;
using IconYuan.Model.DB;
using IconYuan.Model.DTO.Articles;
using IconYuan.Model.Entity.Articles;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System.Linq.Expressions;

namespace IconYuan.Api.Controllers.Articles
{
    /// <summary>
    /// 文章/文章分类操作
    /// </summary>
    public class ArticleController : ApiBase
    {
        readonly ArticleManager articleSvc;
        readonly ArticleCateManager cateSvc;
        public ArticleController()
        {
            articleSvc=new ArticleManager();
            cateSvc=new ArticleCateManager();
        }
        /// <summary>
        /// 文章列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<PageList<ArticleBase>> List(int categoryId, int page = 1, int pageSize = 15)
        {
            var exp = Expressionable.Create<ArticleBase>();
            exp.And(x => x.store_id == GetStoreId());
            exp.AndIF(categoryId > 0, x => x.category_id == categoryId);
            return await articleSvc.GetPageListAsync(exp.ToExpression(), y => y.sort, OrderByType.Asc, page);

            //var json = "{\"list\":{\"total\":2,\"per_page\":15,\"current_page\":1,\"last_page\":1,\"data\":[{\"article_id\":10002,\"title\":\"为何新iPhone降至4999也没热度？有人表示不如旅游或买服装\",\"show_type\":10,\"category_id\":10001,\"image_id\":10216,\"sort\":100,\"status\":1,\"virtual_views\":100,\"actual_views\":9,\"image_url\":\"20190119125731fa15b3797.jpg\",\"category\":{\"category_id\":10001,\"name\":\"手机\",\"status\":1,\"sort\":100},\"show_views\":109},{\"article_id\":10003,\"title\":\"麒麟980+8G+徕卡三摄，这款才是华为“最强旗舰”，别听导购忽悠\",\"show_type\":10,\"category_id\":10001,\"image_id\":10215,\"sort\":100,\"status\":1,\"virtual_views\":100,\"actual_views\":33,\"image_url\":\"20190119125452842158477.jpg\",\"category\":{\"category_id\":10001,\"name\":\"手机\",\"status\":1,\"sort\":100},\"show_views\":133}]}}";
            //return JsonHelper.ToObject<ArticleListDto>(json);
        }

        /// <summary>
        /// 文章详情
        /// </summary>
        /// <param name="article_id">文章id</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<object> Detail(int article_id)
        {
            var result = await articleSvc.GetAsync(article_id);

            return result;
            //var json = "{\"list\":{\"total\":2,\"per_page\":15,\"current_page\":1,\"last_page\":1,\"data\":[{\"article_id\":10002,\"title\":\"为何新iPhone降至4999也没热度？有人表示不如旅游或买服装\",\"show_type\":10,\"category_id\":10001,\"image_id\":10216,\"sort\":100,\"status\":1,\"virtual_views\":100,\"actual_views\":9,\"image_url\":\"20190119125731fa15b3797.jpg\",\"category\":{\"category_id\":10001,\"name\":\"手机\",\"status\":1,\"sort\":100},\"show_views\":109},{\"article_id\":10003,\"title\":\"麒麟980+8G+徕卡三摄，这款才是华为“最强旗舰”，别听导购忽悠\",\"show_type\":10,\"category_id\":10001,\"image_id\":10215,\"sort\":100,\"status\":1,\"virtual_views\":100,\"actual_views\":33,\"image_url\":\"20190119125452842158477.jpg\",\"category\":{\"category_id\":10001,\"name\":\"手机\",\"status\":1,\"sort\":100},\"show_views\":133}]}}";
            //return JsonHelper.ToObject<ArticleDto>(json);
        }


        #region 文章分类
        /// <summary>
        /// 分类列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<ArticleCate>> CategoryList()
        {
            return await cateSvc.GetListAsync(string.Empty);
            //var json = "{\"list\":[{\"category_id\":10001,\"name\":\"手机\",\"status\":1,\"sort\":100},{\"category_id\":10002,\"name\":\"汽车\",\"status\":1,\"sort\":100},{\"category_id\":10003,\"name\":\"型男\",\"status\":1,\"sort\":100},{\"category_id\":10004,\"name\":\"数码\",\"status\":1,\"sort\":100},{\"category_id\":10005,\"name\":\"运动\",\"status\":1,\"sort\":100},{\"category_id\":10006,\"name\":\"国际\",\"status\":1,\"sort\":100}]}";
            //return JsonHelper.ToObject<ArticleCateListDto>(json);
        }
        #endregion
    }
}
